﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;
using DataDefinition;

namespace DALLayer.Providers
{
    public class UserDataProvider: DataAccessBase
    {
        #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="UserDataProvider" /> class.
        /// </summary>
        public UserDataProvider(){ }
        #endregion

        #region [Public Methods]

        /// <summary>
        /// Get user details by id or all users
        /// </summary>
        /// <param name="userId">user id</param>
        /// <param name="selectById">select by id</param>
        /// <returns>data set</returns>
        public AutoFixDataSet GetUserDetails(int userId, bool selectById)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT ApplicationUser.* FROM ApplicationUser");
                //if its a selection by id
                if (selectById == true)
                {
                    //Add the sql string to retrieve data based on ArticleID
                    base.CommandTextBuilder.Append(@" WHERE (UserID = @UserID)");
                    //Add the params needed to retrieve data
                    base.DBParams.Add(@"@UserID", userId);
                }

                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.ApplicationUser.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }
        /// <summary>
        /// Get user details by article id
        /// </summary>
        /// <param name="articleId">article id</param>
        /// <returns>data set</returns>
        public AutoFixDataSet GetUserByArticleID(int articleId)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT ApplicationUser.UserName, ApplicationUser.ProfilePictureURL, ApplicationUser.UserID");
                base.CommandTextBuilder.Append(@" FROM (ApplicationUser INNER JOIN
                         Article ON ApplicationUser.UserID = Article.UserID)"); 
                //Add the sql string to retrieve data based on ArticleID
                base.CommandTextBuilder.Append(@" WHERE (ApplicationUser.IsActive = true) AND (Article.IsActive = true) AND (Article.ArticleID = @ArticleID)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@ArticleID", articleId);

                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.GetUserArticleID.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }

        /// <summary>
        /// Authenticates the user.
        /// </summary>
        /// <param name="email">email address</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public AutoFixDataSet AuthenticateUser(string email, string password)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                base.InitializeDataAccess();

                base.CommandTextBuilder.Append(@"SELECT ApplicationUser.* FROM ApplicationUser");
                base.CommandTextBuilder.Append(@" WHERE (EmailAddress = @EmailAddress) AND (Password = @Password)");

                base.DBParams.Add(@"@EmailAddress", email);
                base.DBParams.Add(@"@Password", password);

                bool success = false;

                success = base.ExecuteReader();

                if (success == true)
                {
                    dataSet = new AutoFixDataSet();
                    dataSet.ApplicationUser.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                //Log here
            }
            finally
            {
                base.FinalizeConnection();
            }

            return dataSet;
        }
        /// <summary>
        /// The method inserts user to the table
        /// </summary>
        /// <param name="firstName">first name</param>
        /// <param name="lastName">last name</param>
        /// <param name="email">email</param>
        /// <param name="userName">user name</param>
        /// <param name="password">password</param>
        /// <param name="profilePictureUrl">profile picture url</param>
        /// <param name="city">city</param>
        /// <param name="country">country</param>
        /// <param name="phone">phone</param>
        /// <param name="userTypeId">usertype id</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool InsertUser(string firstName, string lastName, string email, string userName, string password, string profilePictureUrl, string city, string country, string phone, int userTypeId, bool isActive)
        {

            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append("INSERT INTO ApplicationUser(FirstName, LastName, EmailAddress, UserName, [Password], ProfilePictureURL, City, Country, PhoneNumber, UserTypeID, IsActive) VALUES (@FirstName, @LastName, @EmailAddress, @UserName, @Password, @ProfilePictureURL, @City, @Country, @PhoneNumber, @UserType, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@FirstName", firstName);
                base.DBParams.Add(@"@LastName", lastName);
                base.DBParams.Add(@"@EmailAddress", email);
                base.DBParams.Add(@"@UserName", userName);
                base.DBParams.Add(@"@Password", password);
                base.DBParams.Add(@"@ProfilePictureURL", profilePictureUrl);
                base.DBParams.Add(@"@City", city);
                base.DBParams.Add(@"@Country", country);
                base.DBParams.Add(@"@PhoneNumber", phone);
                base.DBParams.Add(@"@UserType", userTypeId);
                base.DBParams.Add(@"@IsActive", isActive);
                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }

        /// <summary>
        /// The method updates user
        /// </summary>
        /// <param name="firstName">first name</param>
        /// <param name="lastName">last name</param>
        /// <param name="email">email</param>
        /// <param name="userName">user name</param>
        /// <param name="password">password</param>
        /// <param name="profilePictureUrl">profile picture url</param>
        /// <param name="city">city</param>
        /// <param name="country">country</param>
        /// <param name="phone">phone</param>
        /// <param name="userTypeId">usertype id</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool UpdateUser(int userId, string firstName, string lastName, string email, string userName, string password, string profilePictureUrl, string city, string country, string phone, int userTypeId, bool isActive)
        {
            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    UPDATE ApplicationUser
                    SET FirstName = @FirstName,
                        LastName = @LastName,
                        EmailAddress = @EmailAddress,
                        UserName = @UserName,
                        [Password] = @Password,
                        ProfilePictureURL = @ProfilePictureURL,
                        City = @City, 
                        Country = @Country, 
                        PhoneNumber = @PhoneNumber, 
                        UserTypeID =  @UserType,
                        IsActive = @IsActive                       
                        WHERE ([UserID] = @UserID)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@FirstName", firstName);
                base.DBParams.Add(@"@LastName", lastName);
                base.DBParams.Add(@"@EmailAddress", email);
                base.DBParams.Add(@"@UserName", userName);
                base.DBParams.Add(@"@Password", password);
                base.DBParams.Add(@"@ProfilePictureURL", profilePictureUrl);
                base.DBParams.Add(@"@City", city);
                base.DBParams.Add(@"@Country", country);
                base.DBParams.Add(@"@PhoneNumber", phone);
                base.DBParams.Add(@"@UserType", userTypeId);
                base.DBParams.Add(@"@IsActive", isActive);
                base.DBParams.Add(@"@UserID", userId);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;

        }
        #endregion
    }
}
